package com.ship.contract.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.BaseController;
import com.ship.common.core.base.Page;
import com.ship.contract.service.ExportService;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.common.core.utils.SpringUtil;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.ConExcellentCases;
import com.ship.contract.bean.ConRefueling;
import com.ship.contract.service.ConRefuelingService;
import com.ship.contract.task.ExportContractDataTask;
import com.ship.contract.utils.ModuleConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/conRefueling")
public class ConRefuelingController extends BaseController {

    @Autowired
    private ConRefuelingService conRefuelingService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private SpringUtil springUtil;

    /**
     * 分页
     *
     * @param conRefueling
     * @return
     */
    @PostMapping("/page")
    public JsonResult<Page<ConRefueling>> page(@RequestBody ConRefueling conRefueling) {
        Page<ConRefueling> page = conRefuelingService.page(conRefueling,new QueryWrapper<ConRefueling>().ne("del_apply",2));
        return JsonResult.success(page);
    }

    /**
     * 列表查询
     *
     * @param conRefueling
     * @return
     */
    @PostMapping("/list")
    public JsonResult<List<ConRefueling>> getList(@RequestBody ConRefueling conRefueling) {
        List<ConRefueling> page = conRefuelingService.pageExportList(conRefueling,new QueryWrapper<ConRefueling>().ne("del_apply",2),null);
        return JsonResult.success(page);
    }

    /**
     * 新增
     *
     * @param conRefueling
     * @return
     */
    @PostMapping
    @Log(title = "新增加油合同",businessType = BusinessType.INSERT)
    public JsonResult<Object> add(@RequestBody @Validated(Add.class)ConRefueling conRefueling) {
        conRefuelingService.saveCon(conRefueling);
        return JsonResult.success();
    }

    /**
     * 修改
     *
     * @param conRefueling
     * @return
     */
    @PutMapping
    @Log(title = "修改加油合同",businessType = BusinessType.UPDATE)
    public JsonResult<Object> update(@RequestBody @Validated(Update.class)ConRefueling conRefueling) {
        conRefuelingService.updateCon(conRefueling);
        return JsonResult.success();
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除加油合同",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable Long id){
        conRefuelingService.delApply(id);
        return JsonResult.success();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping
    public JsonResult<Map<String,Object>> detail(Long id) {
        return JsonResult.success(conRefuelingService.detail(id));
    }

    /**
     * 申请优秀案例
     * @param conExcellentCases
     * @return
     */
    @PostMapping("/excellent")
    public JsonResult excellent(@RequestBody ConExcellentCases conExcellentCases){
        conRefuelingService.excellent(conExcellentCases, SecurityUtils.getUsername());
        return JsonResult.success();
    }


    /**
     * 导出
     *
     * @param conRefueling
     * @return
     */
    @PostMapping("/export")
    @Log(title = "导出加油合同",businessType = BusinessType.EXPORT)
    public JsonResult<Object> export(@RequestBody ConRefueling conRefueling) {
        ExportContractDataTask task = new ExportContractDataTask();
        task.setBaseBean(conRefueling);
        task.setBaseService(conRefuelingService);
        task.setMethodsName("page");
        task.setModule(ModuleConst.CON_REFUELING);
        task.setDownloadFileName("导出加油合同.zip");
        task.setHttpRequestUtil(httpRequestUtil);
        task.setSpringUtil(springUtil);
        task.setUserId(SecurityUtils.getUserId());
        exportService.export(task);
        return JsonResult.success();
    }
}
